using System;
using System.Collections.Generic;
using Framework.Save;
using LitJson;
using TMPro;
using UnityEngine;

namespace GameCore
{
    public class WarehouseInventory : GridInventory, IJsonSerializable
    {
        public ClickableObject clickableNext;
        public TMP_Text txt_Page;
        public ClickableObject clickablePrev;
        public Vector2Int size = new Vector2Int(12, 12);
        public int pageIndex;
        public List<InventoryData> pages = new();
        public bool initialized = false;

        private void Awake()
        {
            shape = new Shape();
            for (int x = 0; x < size.x; x++)
            {
                for (int y = 0; y < size.y; y++)
                {
                    shape.AddCell(new Vector2Int(x, y));
                }
            }
        }

        protected virtual void Start()
        {
            base.Start();
            clickableNext.OnClick += NextPage;
            clickablePrev.OnClick += PrevPage;
            if (pages.Count == 0)
            {
                pages.Add(new InventoryData(resourceID, shape));
                base.SetData(pages[0]);
            }
        }
        
        public void NextPage()
        {
            pageIndex++;
            if (pages.Count <= pageIndex) CreateNewPage();
            base.SetData(pages[pageIndex]);
            txt_Page.text = $"{pageIndex + 1} / {pages.Count}";
        }

        public void PrevPage()
        {
            pageIndex--;
            if (pageIndex < 0) pageIndex = 0;
            base.SetData(pages[pageIndex]);
            txt_Page.text = $"{pageIndex + 1} / {pages.Count}";
        }

        private void CreateNewPage()
        {
            if (data.IsEmpty())
                pageIndex--;
            else
            {
                pages.Add(new InventoryData(resourceID, shape));
            }
            txt_Page.text = $"{pageIndex + 1} / {pages.Count}";
        }

        public override void SetData(GameData newData)
        {
            var obj = newData as ObjectData;
            pageIndex = obj["page_index"];
            foreach (var page in (ListData)obj["pages"])
                pages.Add(page as InventoryData);
            base.SetData(pages[pageIndex]);
        }

        public override GameData GetData()
        {
            var result = new ObjectData();
            result["page_index"] = pageIndex;
            var list = new ListData();
            foreach (var page in pages)
                list.Add(page);
            return list;
        }

        public JsonData Serialize()
        {
            var json = JsonUtil.NewObject();
            json["page_index"] = pageIndex;
            json["pages"] = JsonUtil.NewArray();
            foreach (var page in pages)
                json.Add(page.Serialize());
            return json;
        }

        public void Deserialize(JsonData json)
        {
            pageIndex = (int)json["page_index"];
            pages.Clear();
            foreach (JsonData jsonPage in json["pages"])
                pages.Add(new InventoryData(jsonPage));
            base.SetData(pages[pageIndex]);
            txt_Page.text = $"{pageIndex + 1} / {pages.Count}";
        }
    }
}